package com.example.steveaoblogdemo.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.steveaoblogdemo.entity.Friend;
import com.example.steveaoblogdemo.mapper.FriendMapper;
import com.example.steveaoblogdemo.service.FriendService;
import com.example.steveaoblogdemo.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FriendServiceImpl extends ServiceImpl<FriendMapper, Friend> implements FriendService {
    @Resource
    FriendMapper friendMapper;

    public Result<?> queryFriend(){
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        List<Friend> list = friendMapper.selectList(queryWrapper);
        return new Result<>(200,"查询成功",list);
    }

    public Result<?> queryFriend(Integer pageNum,Integer pageSize,String search){
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(search)){
            queryWrapper.eq("status",0).like("f_name",search);
        }
        queryWrapper.eq("status",0);
        IPage<Friend> friendIPage = friendMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);
        return new Result<>(200,"查询成功",friendIPage);
    }

    public int addFriend(Integer id,String fName,String fLink){
        friendMapper.addFriend(id,fName,fLink);
        return 0;
    }

    public int updateFriend(String fName,String fLink,Integer id){
        friendMapper.updateFriend(fName,fLink,id);
        return 0;
    }

    public Result<?> deleteFriend(Integer id){
        UpdateWrapper<Friend> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id).set("status",1);
        int result = friendMapper.update(null,wrapper);
        return new Result<>(200,"删除成功",result);
    }

    public Result<?> queryById(Integer id){
        QueryWrapper<Friend> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Friend friend = friendMapper.selectOne(queryWrapper);
        if (friend!=null){
            return new Result<>(200,"查询成功",friend);
        }
        return new Result<>(400,"查询失败",null);
    }
}
